<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table表格区域 -->
      <el-table :data="GoodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column label="商品重量" width="70px" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="140px" prop="add_time">
          <template v-slot="scoped">
            {{scoped.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template v-slot="scoped">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editGoods(scoped.row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
               @click="deleteGoods(scoped.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 100, 300, 600]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
      <!-- 编辑商品信息对话框 -->
  <el-dialog
    title="提示"
    :visible.sync="editDialogVisible"
    width="30%"
    @close="editDialogVisibleClosed"
    >
     <el-form :model="editForm" :rules="editFormRules" ref="editFormRef">
      <el-form-item label="商品名称" prop="goods_name">
        <el-input v-model="editForm.goods_name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格(元)" prop="goods_price">
        <el-input v-model="editForm.goods_price"></el-input>
      </el-form-item>
       <el-form-item label="商品重量" prop="goods_weight" >
        <el-input v-model="editForm.goods_weight"></el-input>
      </el-form-item>
  </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="saveEditGoods">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      /* 商品列表数据 */
      GoodsList: [],
      total: 0,
      /* 编辑商品信息对话框 */
      editDialogVisible: false,
      /* 修改商品信息表单 */
      editForm: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
      },
      /* 修改表单验证规则 */
      editFormRules: {
        goods_name: [
         { required: true, message: '请输入商品名称', trigger: 'blur' },
        ],
        goods_price: [
         { required: true, message: '请输入商品价格', trigger: 'blur' },
        ],
        goods_weight: [
         { required: true, message: '请输入商品重量', trigger: 'blur' },
        ]
      },
      /* 商品信息id */
      goods_id: 0,
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    /* 获取商品列表数据 */
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败!')
      }
      this.GoodsList = res.data.goods
      this.total = res.data.total
      this.$message.success('获取商品数据成功!')
      // 143
    },
    /* pageSize 改变时会触发 */
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    /* currentPage 改变时会触发 */
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsList()
    },
    deleteGoods (row) {
       this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${row.goods_id}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除列表失败!')
        }
        this.getGoodsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /* 跳转到商品添加页面 */
    goAddPage () {
      this.$router.push('/goods/add')
    },
    /* 编辑商品信息 */
    async editGoods (goods) {
      /* 保存商品信息id，方便后面保存商品信息使用 */
      this.goods_id = goods.goods_id
      const { data: res } = await this.$http.get(`goods/${goods.goods_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品信息失败!')
      }
      this.editForm = res.data
      console.log('编辑商品信息',res.data)
      this.editDialogVisible = true
    },
    /* 保存修改的商品信息 */
    saveEditGoods () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return 
        const { data: res } = await
        this.$http.put(`goods/${this.goods_id}`, this.editForm)
        if (res.meta.status !== 200) {
          return this.$message.error('保存商品信息失败!')
        }
        this.editDialogVisible = false
        this.getGoodsList()
      })
    },
    /* 编辑对话框关闭事件 */
    editDialogVisibleClosed () {
      this.$refs.editFormRef.resetFields()
      console.log(123)
    }
  }
}
</script>

<style lang="less" scoped>
</style>